package graduation.claf.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
public class testpic {

    @GetMapping(value = "/portal/user/update.do")
    public String upload(){
        return "upload";
    }

    @Value("${nginx.host}")
    String imgHost;

    @PostMapping(value = "/portal/user/update.do")
    @ResponseBody
    public String upload(@RequestParam("userpic") MultipartFile file){
        //file.getOriginalFilename();获得文件上传的名字
        if (file != null && file.getOriginalFilename() != null){
            //获取文件名
            String originalFilename = file.getOriginalFilename();//xxx.png
            //获取拓展名
            String extend = originalFilename.substring(originalFilename.lastIndexOf("."));
            String  newName = UUID.randomUUID().toString();
            File file1 = new File("/Users/fanfeng/Documents/pngTest",newName+extend);
            try {
                file.transferTo(file1);
                return imgHost+newName+extend;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
